#
# @lc app=leetcode.cn id=21 lang=python3
#
# [21] 合并两个有序链表
#


# @lc code=start
# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1:
            return l2
        elif not l2:
            return l1
        if l1.val < l2.val:
            start = l1
            l1 = l1.next
        else:
            start = l2
            l2 = l2.next
        hand = start
        while l1 and l2:
            if l1.val < l2.val:
                hand.next = l1
                l1 = l1.next
            else:
                hand.next = l2
                l2 = l2.next
            hand = hand.next
        if l1:
            hand.next = l1
        else:
            hand.next = l2
        return start


# @lc code=end
